package com.ruoyi.cmdb.controller;

import com.ruoyi.cmdb.service.MessageService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 测试redis
 *
 */
@RestController
@RequestMapping("/cmdb/testredis")
@Api(tags = "3.testredis")
public class TestRedisController {
    @Autowired
    RedisCache redisCache;

    @Value("${token.expireTime}") //token过期时间
    int expireTime;

    @Autowired
    private MessageService messageService;

    /**
     * testredis send
     */
    @PostMapping("/send/{value}")
    @ApiOperation("send")
    public AjaxResult send(@PathVariable String value)  {
        redisCache.setCacheObject("testRedisKey",value,expireTime, TimeUnit.MINUTES);
        String rps="redis send success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }

    /**
     * testredis get
     */
    @PostMapping("/get/{key}")
    @ApiOperation("get")
    public AjaxResult get(@PathVariable String key)  {
        String value = redisCache.getCacheObject(key);

        String rps="";
        if (Objects.equals(value, "null")){
            rps="redis :"+key+" receive success sout,value:"+value;
        }else{
            rps="redis :"+key+" receive success sout,value:"+value;
        }
        System.out.println(rps);
        return AjaxResult.success(rps);
    }
    /**
     * testredis delete
     */
    @PostMapping("/delete/{key}")
    @ApiOperation("delete")
    public AjaxResult delete(@PathVariable String key)  {
        String value = redisCache.getCacheObject(key);
        String rps="";
        if (value==null){
            rps="redis :"+key+" not exist";
        }else{
            redisCache.deleteObject(key);
            rps="redis :"+key+" delete success sout,value:"+value;
        }
        System.out.println(rps);
        return AjaxResult.success(rps);
    }
}
